function pageResult($scope) {
	if ($scope.offset < 0)
		$scope.offset = 0;

	$scope.a = $scope.a_ini.slice(0 + $scope.offset, $scope.entryLimit
			+ $scope.offset);
	$scope.page_now = ($scope.offset / $scope.entryLimit) + 1;
	return $scope;
}

function pageResult_group($scope) {
	if ($scope.offset_group < 0)
		$scope.offset_group = 0;
	$scope.b = $scope.b_ini.slice(0 + $scope.offset_group,
			$scope.entryLimit_group + $scope.offset_group);
	$scope.page_now_group = ($scope.offset_group / $scope.entryLimit_group) + 1;
	return $scope;
}

function pageResult_group_($scope) {
	if ($scope.offset_group_ < 0)
		$scope.offset_group_ = 0;
	$scope.c = $scope.c_ini.slice(0 + $scope.offset_group_,
			$scope.entryLimit_group_ + $scope.offset_group_);
	$scope.page_now_group_ = ($scope.offset_group_ / $scope.entryLimit_group_) + 1;
	return $scope;
}

function nextPage_common($scope) {
	$scope.offset += $scope.entryLimit;

	if ($scope.nb_row_notchange == $scope.filtered.length) {
		$scope.filtered_ini = angular.copy($scope.filtered);
	}

	if (!angular.isUndefined($scope.filtered_ini)) {
		$scope.recalculate_array($scope.filtered_ini);
	}

	if (!angular.isUndefined($scope.search1)) {

		$scope.recalculate_array($scope.filtered_search);
	} else {
		$scope.jsonToArray($scope.model);
	}

	pageResult($scope);
	$scope.disablePrev = false;

	if (($scope.offset + 1) > ($scope.nbr_rows - $scope.entryLimit))
		$scope.disableNext = true;

	return $scope;
}

function nextPage_common_group($scope) {
	$scope.offset_group += $scope.entryLimit_group;
	pageResult_group($scope);
	$scope.disablePrev_group = false;
	if (($scope.offset_group + 1) > ($scope.nbr_rows_groups - $scope.entryLimit_group))
		$scope.disableNext_group = true;

	return $scope;
}

function nextPage_common_group_($scope) {
	$scope.offset_group_ += $scope.entryLimit_group_;
	pageResult_group_($scope);
	$scope.disablePrev_group_ = false;
	if (($scope.offset_group_ + 1) > ($scope.nbr_rows_members - $scope.entryLimit_group_))
		$scope.disableNext_group_ = true;

	return $scope;
}

function nextPage_group($scope) {
	$scope.offset += $scope.entryLimit;

	if ($scope.nb_row_notchange == $scope.filtered.length) { 
		$scope.filtered_ini = angular.copy($scope.filtered);
	}
	if (!angular.isUndefined($scope.filtered_ini)) { // filtered_ini has some
													// thing
		$scope.recalculate_array($scope.filtered_ini);
	} else {
	    if($scope.type_filter=='most'){
		  $scope.getInterest_populaire($scope.model1);	
		}else{
		$scope.jsonToArray($scope.model);
		}
	}
	// /alert(JSON.stringify($scope.model));
	// alert($scope.nb_row_notchange + ' - ' +$scope.filtered.length + ' - '
	// +$scope.model.data.length);
	pageResult($scope);
  
	$scope.disablePrev = false;

	if (($scope.offset + 1) > ($scope.nbr_rows - $scope.entryLimit))
		$scope.disableNext = true;

	return $scope;
}

function previousPage_group($scope) {
	$scope.offset -= $scope.entryLimit;

	if ($scope.nb_row_notchange == $scope.filtered.length) {
		$scope.filtered_ini = angular.copy($scope.filtered);
	}
	if (!angular.isUndefined($scope.filtered_ini)) {
		$scope.recalculate_array($scope.filtered_ini);
	} else {
		$scope.jsonToArray($scope.model);
	}

	// alert($scope.nb_row_notchange + ' - ' +$scope.filtered.length + ' - '
	// +$scope.filtered_ini.length);

	// $scope.pageResult();
	pageResult($scope);
	$scope.disableNext = false;
	if ($scope.offset == 0)
		$scope.disablePrev = true;

	$scope.click_prev = true;

	return $scope;
}

// switch to previous page
function previousPage_common($scope) {
	$scope.offset -= $scope.entryLimit;

	// alert(JSON.stringify($scope.a));

	if (!angular.isUndefined($scope.search1)) {
		$scope.recalculate_array($scope.filtered_search);
	} else {
		$scope.jsonToArray($scope.model);
	}

	pageResult($scope);

	$scope.disableNext = false;
	if ($scope.offset == 0)
		$scope.disablePrev = true;

	$scope.click_prev = true;

	return $scope;
}

function previousPage_common_group($scope) {
	$scope.offset_group -= $scope.entryLimit_group;
	pageResult_group($scope);

	$scope.disableNext_group = false;
	if ($scope.offset_group == 0)
		$scope.disablePrev_group = true;
	return $scope;
}

function previousPage_common_group_($scope) {
	$scope.offset_group_ -= $scope.entryLimit_group_;
	pageResult_group_($scope);

	$scope.disableNext_group_ = false;
	if ($scope.offset_group_ == 0)
		$scope.disablePrev_group_ = true;
	return $scope;
}

function getdatetime_search(nb_day) {
	var now = new Date();
	var today_datetime = Math.floor(now.getTime());
	today_datetime -= (nb_day * 86400000);
	return today_datetime;
}

function compare(p, val) {

	var today_datetime = getdatetime_search(val);

	return p > today_datetime;
}

function compare_event_with_today(start_time,end_time,type) {
    var now = new Date();
	var today_datetime = Math.floor(now.getTime());
    var type_return = false;
	switch (type) {
    case 1:
    	if( start_time > today_datetime)
    		type_return = true;
        break;
    case 2:
    	if( today_datetime > end_time )
    		type_return = true;
    	break;
    case 3:
    	if ( (today_datetime >= start_time) &&  (today_datetime <= end_time))
    		type_return = true;
    	break;
    case 4:
    	type_return = true;
    	break;
    default:
    	type_return = true;
    }
	
    return type_return;
}

function today(p, val) {

	var today_datetime = getdatetime_search(val);
	// alert(p[5] - today_datetime);
	return p[5] > today_datetime;
}

function count_row(data) {
	var i = 0;
	angular.forEach(data.members_list, function() {
		i++;
	})
	$scope.nbr_count = i;
	return $scope.nbr_count;
}

// -----------------------------------event-------------

function nextPage_event($scope) {
	$scope.offset += $scope.entryLimit;
	pageResult($scope);
	$scope.disablePrev = false;
	
	
	
	if (($scope.offset + 1) > ($scope.nb_row_event - $scope.entryLimit))
		{
		 $scope.disableNext = true;
		}

	return $scope;
}

function previousPage_event($scope) {
	$scope.offset -= $scope.entryLimit;
	$scope.jsonToArray($scope.model,$scope.type_filter_event);
	pageResult($scope);
	$scope.disableNext = false;
	if ($scope.offset == 0)
		$scope.disablePrev = true;

	$scope.click_prev = true;
	return $scope;
}

function pageResult_event($scope) {
	if ($scope.offset_event < 0)
		$scope.offset_event = 0;
	$scope.b = $scope.b_ini.slice(0 + $scope.offset_event,
			$scope.entryLimit_event + $scope.offset_event);
	$scope.page_now_event = ($scope.offset_event / $scope.entryLimit_event) + 1;
	// alert(JSON.stringify($scope.b));

	return $scope;
}

function nextPage_event_users($scope) {
	$scope.offset_event += $scope.entryLimit_event;
	pageResult_event($scope);
	$scope.disablePrev_event = false;
	if (($scope.offset_event + 1) > ($scope.nbr_rows_attendings - $scope.entryLimit_event))
		$scope.disableNext_event = true;

	return $scope;
}

function previousPage_event_users($scope) {
	$scope.offset_event -= $scope.entryLimit_event;
	$scope.jsonToArray_members($scope.lst_user);
	pageResult_event($scope);
	$scope.disableNext_event = false;
	if ($scope.offset_event == 0)
		$scope.disablePrev_event = true;

	return $scope;
}


function pageResult_group_event($scope) {
	if ($scope.offset_eg < 0)
		$scope.offset_eg = 0;
	$scope.d = $scope.d_ini.slice(0 + $scope.offset_eg,
			$scope.entryLimit_eg + $scope.offset_eg);
	$scope.page_now_eg = ($scope.offset_eg / $scope.entryLimit_eg) + 1;
	return $scope;
}

function nextPage_evg($scope) {
	$scope.offset_eg += $scope.entryLimit_eg;
	pageResult_group_event($scope);
	$scope.disablePrev_eg = false;
	if (($scope.offset_eg + 1) > ($scope.nbr_rows_eg - $scope.entryLimit_eg))
		$scope.disableNext_eg = true;

	return $scope;
}

function previousPage_evg($scope) {
	$scope.offset_eg -= $scope.entryLimit_eg;
	$scope.jsonToArray_events($scope.lst_events);
	pageResult_group_event($scope);
	$scope.disableNext_eg = false;
	if ($scope.offset_eg == 0)
		$scope.disablePrev_eg = true;

	return $scope;
}

// -----------------------------------event-------------

function unique(origArr) {
    var newArr = [],
        origLen = origArr.length,
        found, x, y;

    for (x = 0; x < origLen; x++) {
        found = undefined;
        for (y = 0; y < newArr.length; y++) { 
			if (angular.equals(origArr[x],newArr[y])) { 
                found = true;
                break;
            }
        }
        if (!found && origArr[x]) {
            newArr.push(origArr[x]);
        }
    }
	 
    return newArr;
}
